<?php
declare(strict_types = 1);
namespace App\Controller\Api;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Controller\AbstractController;
use App\Amqp\Producer\SelfBbcMessageProducer;
use Hyperf\Amqp\Producer;
use App\Service\Code\Normal;

/**
 * @Controller(prefix="/YunCangGoodsPushMessage")
 * @author chenyun
 */
class YunCangGoodsPushMessageController extends AbstractController
{
    /**
     * 推送自营BBC消息服务功能
     * @RequestMapping(path="PushSelfBbcGoodsMessage", methods="post")
     * @return string[]
     */
    public function pushSelfBbcGoodsMessage(RequestInterface $request)
    {
        $sid = env('sid');
        $body = $request->getParsedBody();
        $data = [
            'sid'           => $sid ? $sid : 1,
            'cid'           => $body['cid'],
            'data'          => $body['data'],
            'MessageType'   => $body['MessageType'],
            'logid'         => $body['MessageId'] ?? ''
        ];
        try
        {
            $message    = new SelfBbcMessageProducer($notifyMessage);
            $producer   = container()->get(Producer::class);
            $result     = $producer->produce($message,true,30);
            if($result) {
                return Normal::returnCode(Normal::SUC,"写入成功");
            }
            logger('info','SelfBbcMessageProducerMQError')->info("发布消息失败");
            return Normal::returnCode(Normal::FAIL,"发布消息失败");
        } catch ( \Throwable $th ) {
            logger('info','SelfBbcMessageProducerMQError')->info($th->getMessage());
            return Normal::returnCode(Normal::FAIL,$th->getMessage());
        }
    }
}